package it.okkam.rivelamaps;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vividsolutions.jts.geom.Point;

/**
 * Servlet implementation class myServlet
 */
public class myServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public myServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//String nome = req.getParameter("nome");
	
		String valoreScelto = req.getParameter("op");
		String tipoScelta = req.getParameter("op2");
		
		ParticelleConCambiamento pcc = new ParticelleConCambiamento();
		Mappa mappa = new Mappa();
		
		mappa.getZoomLevel();
		res.getWriter().write("<html>");
		res.getWriter().write("<head>");
		
		/*ADD */
		//res.getWriter().write("<select>");
			
				
		res.getWriter().write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
		res.getWriter().write("<script type=\"text/javascript\" src=\"http://maps.googleapis.com/maps/api/js?sensor=false\"></script>");
		res.getWriter().write("<title>Rivela view</title>");

		res.getWriter().write("<style type=\"text/css\">" +
		 "html { height: 100% }" +
		 "body { height: 100%; margin: 0; padding: 0 }" +
		"#map_canvas { height: 100% }" +
		"</style> ");
		System.out.println(valoreScelto + "ciao" );
		System.out.println(tipoScelta);
		if (tipoScelta.equalsIgnoreCase("comune")){
			valoreScelto = mappa.convertiComune(valoreScelto);
		}
		
		System.out.println(valoreScelto);
		
		if(pcc.getParticelleCambiamentoCodCom(valoreScelto).size() == 0){
			res.sendRedirect("index.html?mod=no");
			
		}else{
			
			System.out.println(pcc.getParticelleCambiamentoCodCom(valoreScelto).size());
			if ((pcc.getParticelleCambiamentoCodCom(valoreScelto).size())!=0){;
			System.out.println(pcc.getParticelleCambiamentoCodCom(valoreScelto).size());
			//funzioni che generano la mappa
			res.getWriter().write("<script type=\"text/javascript\">");
			res.getWriter().write(
			"function printMap(){" +
			"var la = "+mappa.getCentroLa(valoreScelto)+";" +
			"var lo = "+mappa.getCentroLo(valoreScelto)+";" +
			"var latlng = new google.maps.LatLng(la, lo);" +
			"var myOptions = {zoom:"+ mappa.getZoomLevel()+", center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };" +
			"var map = new google.maps.Map(document.getElementById(\"map_canvas\") ,myOptions);");
			for(int i=0;i<pcc.getParticelleCambiamentoCodCom(valoreScelto).size();i++){
			
				String la1 = Double.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getArrayPunti().get(0).getX());
				String lo1 = Double.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getArrayPunti().get(0).getY());
				
				Point centroPcc = pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getCentro();	
				String loC = Double.toString(centroPcc.getX());
				String laC = Double.toString(centroPcc.getY());
				
				String codiceComune = "Cod. Comune: "+ pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getCodiceComune();
				
				String numeroPcc = "Numero: "+ Integer.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getNumero());
				String denominatorePcc =Integer.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getDenominatore());
				String cambiamento = pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getCDType();
				String codiceCamb = "Tipo cambiamento: " + cambiamento;
				String testo = codiceComune+ "<br />" + numeroPcc + "/"+ denominatorePcc + "<br />" + codiceCamb;  
				
				String colore = "#000000";
				if (cambiamento.equals("A"))
					colore = "#ff0000";
				if (cambiamento.equals("B"))	
					colore = "#00ff00";
				if (cambiamento.equals("C"))
					colore = "#0000ff";
			
				//res.getWriter().write("var testoMarker"+i+" = " + testo +";"); 
				res.getWriter().write("var laM = "+loC+";");
				res.getWriter().write("var loM = "+laC+";");
				res.getWriter().write("var latlngM = new google.maps.LatLng(laM, loM);");
				
				res.getWriter().write("var poligono"+i+";");
				// MARKER
				res.getWriter().write("var infowindow"+i+" = new google.maps.InfoWindow();");
				res.getWriter().write("infowindow"+i+".setContent(\""+testo+"\");");
			
				res.getWriter().write("var marker"+i+" = new google.maps.Marker({"+
						"position: latlngM,"+
						"map: map,"+
			      		"title:\"Rivela Maps\"});");	
				res.getWriter().write("google.maps.event.addListener(marker"+i+", 'click', function() {"+
						"infowindow"+i+".open(map,marker"+i+");"+
					"});");
			
			
			
				res.getWriter().write("var poligonoCoord"+i+" = [ ");
				
				for(int j=0;j<pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getArrayPunti().size();j++){
					res.getWriter().write("new google.maps.LatLng("+Double.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getArrayPunti().get(j).getX())+","+ Double.toString(pcc.getParticelleCambiamentoCodCom(valoreScelto).get(i).getArrayPunti().get(j).getY())+"),");
				}
				res.getWriter().write("new google.maps.LatLng(" + la1 + "," + lo1 +")];");
				res.getWriter().write("poligono"+i+"=new google.maps.Polygon({" +
						"paths: poligonoCoord"+i+"," +
						//"strokeColor: "+ colore +"," +
						"strokeColor: \""+colore+"\"," +
						"strokeOpacity: 0.8," +
						"strokeWeight: 2," +
						"fillColor: \""+colore+"\"," +
						//"fillColor: "+ colore +"," +
						"fillOpacity: 0.35 " +
						"});" +
						"poligono"+i+".setMap(map);");
				}
			
			res.getWriter().write("}");
			res.getWriter().write("</script>");
			res.getWriter().write("</head>");
			res.getWriter().write("<body onload=\"printMap();\">");}
			res.getWriter().write("<div id=\"map_canvas\" style=\"width:100%; height:100%\">");
			res.getWriter().write("</div>");
			res.getWriter().write("</body>");
			res.getWriter().write("</html>");
			
			
		}

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
